<?php

namespace common\models;

use Yii;
use common\helpers\Util;

/**
 * This is the model class for table "UserUpload".
 */
class UserUpload extends ActiveRecord {

    // @name user login client defined
    const TypeDesign = 0;
    public static $clientSelector = [
        self::TypeDesign => ['title' => '审核作品', 'status' => 'purple'],
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['user_id', 'type', 'image'], 'required'],
            [['user_id', 'type'], 'integer'],
            [['image'], 'string', 'max' => 255],
        ];
    }
    /**
     * @name 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'user_id' => '用户',
            'type' => '类型',
            'image' => '图片地址',
        ];
    }

    /**
     * @name 获取user信息
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['user_id' => 'id']);
    }

    /**
     * @name 删除文件及记录
     * @return boolean
     */
    public function delete()
    {
        if(parent::delete()) {
            Util::trashUpload($this->image);
            return true;
        }
        return false;
    }
}